﻿using System;
using System.Collections.Generic;
using FrankJie.Model;
using FrankJie.IDAL;
using FrankJie.Cache;

namespace FrankJie.BLL
{
    public class Tag
    {
        private readonly static ITag dal = DataAccess.CreateTag();

        /// <summary>
        /// 增加一个标签
        /// </summary>
        /// <param name="tagInfo">标签实体</param>
        /// <returns>返回标签ID</returns>
        public static int Add(TagInfo tagInfo)
        {
            int id = dal.Add(tagInfo);
            if (id > 0)
            {
                CacheStrategy.RemoveObject(CacheKey.TAG_LIST);
            }
            return id;
        }

        /// <summary>
        /// 增加标签所拥有的数量
        /// </summary>
        /// <param name="id">标签ID</param>
        /// <returns>返回是否成功</returns>
        public static bool AddCount(int id)
        {
            bool isSuccess = dal.AddCount(id);
            if (isSuccess)
            {
                CacheStrategy.RemoveObject(CacheKey.TAG_LIST);
            }
            return isSuccess;
        }

        /// <summary>
        /// 减少标签所拥有的数量
        /// </summary>
        /// <param name="id">标签ID</param>
        /// <returns>返回是否成功</returns>
        public static bool DeductCount(int id)
        {
            bool isSuccess = dal.DeductCount(id);
            if (isSuccess)
            {
                CacheStrategy.RemoveObject(CacheKey.TAG_LIST);
            }
            return isSuccess;
        }

        /// <summary>
		/// 删除标签通过ID
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
        public static bool Delete(int id)
        {
            bool isSuccess = dal.Delete(id);
            if (isSuccess)
            {
                CacheStrategy.RemoveObject(CacheKey.TAG_LIST);
            }
            return isSuccess;
        }

        /// <summary>
        /// 获取标签列表
        /// </summary>
        /// <returns>返回标签集合</returns>
        public static List<TagInfo> GetList()
        {
            List<TagInfo> tagInfoList = new List<TagInfo>();
            tagInfoList = (List<TagInfo>)CacheStrategy.GetObject(CacheKey.TAG_LIST);
            if (tagInfoList == null)
            {
                tagInfoList = dal.GetList();
            }
            return dal.GetList();
        }
    }
}
